from apps.common.base.dao import BaseDao
from apps.user.serializers import *
from django.db.models import F
from apps.system.rank.dao import RankDao
import datetime

class UserDao(BaseDao):
    MODEL_CLASS = UserModel
    SERI_CLASS = UserBaseSerializer

    @staticmethod
    def addWallet(scene, obj, value):
        print('增加积分', scene, obj, value)
        if scene == 'incomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomes = F('incomes') + value
            obj.save()
        elif scene == 'historyIncomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.historyIncomes = F('historyIncomes') + value
            obj.save()

    def subWallet(self, scene, obj, value):
        print('减少积分', scene, obj, value)
        if scene == 'incomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomes = F('incomes') - value
            obj.save()
        elif scene == 'historyIncomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.historyIncomes = F('historyIncomes') - value
            obj.save()

    # 更新员工等级
    def updateRankByUserAndGrade(self, user, grade):
        if not user.lessee:
            print('用户没有lessee')
            return
        _user = UserDao().findOneByObjId(user.id)
        _user.rank = RankDao().findOneByLesseeIdAndGrade(user.lessee.id, grade)
        _user.upRankTime = datetime.datetime.now()
        _user.save()